package service

import (
    "gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
	"encoding/json"
)

type ProductCustomFieldService interface {
	ListProductCustomField(ctx context.Context, req *api.ListProductCustomFieldRequest) (*api.ListProductCustomFieldResponse, error)
	GetProductCustomField(ctx context.Context, req *api.ProductCustomFieldData) (*api.ProductCustomFieldData, error)
	AddProductCustomField(ctx context.Context, req *api.ProductCustomFieldData) error
	EditProductCustomField(ctx context.Context, req *api.ProductCustomFieldData) error
	DeleteProductCustomField(ctx context.Context, id int64) (err error)
	BatchDeleteProductCustomField(ctx context.Context, req *api.BatchDeleteProductCustomFieldRequest) (err error)
}

type somsService struct {
	productCustomFieldRepo repo.ProductCustomFieldRepo
}

func (s *somsService) ListProductCustomField(ctx context.Context, req *api.ListProductCustomFieldRequest) (*api.ListProductCustomFieldResponse, error) {
	dbDatas, count, err := s.productCustomFieldRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.ProductCustomFieldDBDataParam{
	    ProductCustomFieldDBData: repo.ProductCustomFieldDBData {
            Name: req.Data.Name,
			Explain: req.Data.Explain,
			Type: req.Data.Type,
			RequiredFlag: req.Data.RequiredFlag,
			PrintFlag: req.Data.PrintFlag,
			Sort: req.Data.Sort,
			OptionValue: req.Data.OptionValue,
			
	    },
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.ProductCustomFieldData
	if dbDatas != nil {
		result = make([]*api.ProductCustomFieldData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.ProductCustomFieldData{
				Id:                  data.Id,
                OrganizationId:      data.OrganizationId,
                CreatedAt:           util.FormatDateTime(data.CreatedAt),
                UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
                CreatedUserId:       data.CreatedUserId,
                UpdatedUserId:       data.UpdatedUserId,
                UpdatedUserNickname: data.UpdatedUserNickname,
                CreatedUserNickname: data.CreatedUserNickname,
				Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
			}
		}
	}
	return &api.ListProductCustomFieldResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetProductCustomField(ctx context.Context, req *api.ProductCustomFieldData) (*api.ProductCustomFieldData, error) {
	data, err := s.productCustomFieldRepo.Get(ctx, &repo.ProductCustomFieldDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: req.Id,
        			},
        		},
		})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.ProductCustomFieldData{
		Id:                  data.Id,
        OrganizationId:      data.OrganizationId,
        CreatedAt:           util.FormatDateTime(data.CreatedAt),
        UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
        CreatedUserId:       data.CreatedUserId,
        UpdatedUserId:       data.UpdatedUserId,
        UpdatedUserNickname: data.UpdatedUserNickname,
        CreatedUserNickname: data.CreatedUserNickname,
		Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
	}
	return resp, nil
}

func (s *somsService) AddProductCustomField(ctx context.Context, data *api.ProductCustomFieldData) error {
	repoData := &repo.ProductCustomFieldDBData{
		Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
	}
	_, err := s.productCustomFieldRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditProductCustomField(ctx context.Context, data *api.ProductCustomFieldData) error {
	repoData := &repo.ProductCustomFieldDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: data.Id,
        			},
        		},
		Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
	}
	return handler.HandleError(s.productCustomFieldRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteProductCustomField(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.productCustomFieldRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteProductCustomField(ctx context.Context, req *api.BatchDeleteProductCustomFieldRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
